var open_urls = "";
var g_parsingUrl = "";
var app_pkname = "";
var uploadType = ""
var uploadText = "";
var openAppInfo = function(pn){
	app_pkname = pn;
	$("#app_popupBox").fadeIn();
};
var startApp = function(){
	$.get("/app_start?pn=" + app_pkname, function(e){}, "json");
	$("#app_popupBox").fadeOut();
};
var delApp = function(){
	$.get("/app_del?pn=" + app_pkname, function(e){}, "json");
	$("#app_popupBox").fadeOut();
};
var appInfo = function(){
	$.get("/app_action?pn=" + app_pkname, function(e){}, "json");
	$("#app_popupBox").fadeOut();
};
var tzurl_file = function(){
	window.location.href = "/fb?path=/";
};
var tzurl_videolist = function(cid){
	window.location.href = "/my.html?cid=" + cid;
};
var refAppList = function(){
	$.ajax({
		url: "/app_list",
		timeout : 3000,
		type : "GET",
		dataType: "json",
		success: function(e){
			if (e.suc == 0) {
				var toAppStr = "";
				var applist = e.list;
				$("#dev_ip").text("IP：" + e.ip);
				$("#model").text("我的" + e.model);
				$("#dev_mac").text("MAC：" + e.mac);
				$("#dev_area").text("位置：" + e.area);
				$("#dev_uuid").text("设备ID：" + e.uuid);
				$("#dev_appcode").text("版本号：" + e.appcode);
				$("#dev_appver").text("版本信息：" + e.appver);
				$("#dev_pkname").text("应用包名：" + e.pkname);
				$("#dev_jarver").text("插件版本：" + e.jarver);
				$("#dev_channel").text("渠道ID：" + (e.channel == "tv" ? "官方版" : e.channel));
				for(var i=0;i < applist.length;i++){
					var d = applist[i];
					toAppStr += "<a class=\"programme_img_box\" href=\"javascript:\" onclick=\"openAppInfo('" + d.packagename + "')\">";
					toAppStr += "<img src=\"/app_icon?pn="+d.packagename+"\" />";
					toAppStr += "<div class=\"inner\"><h3>" + d.appname + "</h3><p>版本号：" + d.appcode + "</p><p>版本信息：" + d.appver + "</p></div></a>";
				}
				$("#app_list").html(toAppStr);
			}
		}
	});
};
var upload_url = function(){
	$("#intall_popupBox").fadeOut();
	var apkurl = prompt("输入APK下载连接：", "");
	if (!common.utils.isEmpty(apkurl)) {
		if (apkurl.length > 7) {
			if (apkurl.indexOf("://") != -1) {
				$.get("/app_install?url=" + encodeURIComponent(apkurl), function(e){
					alert("下载任务已建立,稍后请查看客户端提示！");
				}, "json");
			} else {
				alert("输入的下载URL不合法,请检查无误后再操作!");
			}
		}
	}
};
var set_tvurl = function(){
	$("#uptxt_popupBox").fadeOut();
	var tvurl = prompt("输入网络TXT文本连接：", "http://cloud.52itv.cn/tv.txt");
	if (!common.utils.isEmpty(tvurl)) {
		if (tvurl.length > 7) {
			if (tvurl.indexOf("://") != -1) {
				$.get("/set_tvurl?url=" + encodeURIComponent(tvurl), function(e){
					if (e.suc == 0) {
						alert("已成功读取网络自定义列表,请打开频道列表查看！");
					} else {
						alert("读取网络自定义列表失败!");
					}
				}, "json");
			} else {
				alert("输入的自定义URL不合法,请检查无误后再操作!");
			}
		}
	}
};
var del_tvlist = function(){
	$("#uptxt_popupBox").fadeOut();
	$.get("/del_tvlist?t=" + Math.random(), function(e){ alert("自定义列表已清空,请查看客户端提示!") }, "json");
};
var addFav = function(url){
	$("#info_popupBox").fadeOut();
	$.get("/fav_add?cid=1&name=-&url=" + encodeURIComponent(url), function(e){}, "text");
};
var uploadFile = function(uri, files){
	var fd = new FormData();
	fd.append("myfile", files);
	$.ajax({url: uri, type: "POST", processData: false, contentType: false, data: fd, dataType: "json", success: function(e) { alert(e.msg) }});
};
var init_search = function(){
	var url = $("#searchInput").val();
	if (open_urls != url) {
		if (url.indexOf("://") != -1) {
			open_urls = url;
			$("#searchInput").val("");
			$.get("/video?url=" + encodeURIComponent(url), function(str){
				if (str.indexOf("://") != -1) {
					addFav(url);
				}
			}, "text");
		}
	}
};

var do_Parse = function(){
    var url = $("#parseInputHtml").val();
    if (g_parsingUrl != url) {
        if (url.indexOf("://") != -1) {
            g_parsingUrl = url;
            $("#parseInputHtml").val("");
            $.get("/parseurl?url=" + encodeURIComponent(url), function(str){
                console.log(str)
            }, "text");
        }
    }
};

$(function(){
	var r = common.utils.getUrlParams();
	$("#btn_cancle").click(function(){
		$("#info_popupBox").fadeOut();
	});
	$("#app_cancle").click(function(){
		$("#app_popupBox").fadeOut();
	});
	$("#show_devinfo").click(function(){
		$("#info_popupBox").fadeIn();
	});
	$("#myfile").change(function(e){
		var files = this.files[0];
		if (files.name.indexOf(uploadType) > 1) {
			if (uploadType == ".apk"){
				uploadFile("/upload", files);
			} else if (uploadType == ".txt"){
				uploadFile("/tv", files);
			}
		} else {
			alert(uploadText);
		}
	});
	$("#upload_show").click(function(){
		$("#intall_popupBox").fadeIn();
	});
	$("#upload_out").click(function(){
		$("#intall_popupBox").fadeOut();
	});
	$("#tvlist_show").click(function(){
		$("#uptxt_popupBox").fadeIn();
	});
	$("#upload_txtout").click(function(){
		$("#uptxt_popupBox").fadeOut();
	});
	$("#upload_apk").click(function(){
		$("#intall_popupBox").fadeOut();
		uploadType = ".apk";
		uploadText = "请上载后缀名为 *.apk 的应用文件.";
		$("#myfile").get(0).click();
	});
	$("#upload_tvlist").click(function(){
		$("#uptxt_popupBox").fadeOut();
		uploadType = ".txt";
		uploadText = "请上载后缀名为 *.txt 的文本文件.";
		$("#myfile").get(0).click();
	});
	$("#searchInput").keyup(function(){
		if(event.keyCode == 13){
			init_search();
		}
	});
	$("#searchInput").blur(function(){
		init_search();
	});

	$("#parseInputHtml").keyup(function(){
	    if(event.keyCode == 13){
            do_Parse();
        }
	});

	$("#parseInputHtml").blur(function(){
    		do_Parse();
    });

	if (!common.utils.isEmpty(r.ver)) {
		$.get("/close_qrcode.js", function(e){}, "text");
	}
	var ch_index = 0;
	if (!common.utils.isEmpty(r.i)) {
		ch_index = parseInt(r.i);
	};
	$("#cate_slideList").children("section").eq(ch_index).css("display", "block");
	$("#cate_slider").css("-webkit-transform", "translateX(" + ch_index * 100 + "%)");
	$("#cate_menu").delegate("li", "click", function(){
		$(this).parent().find("li").removeClass('on');
		$(this).addClass("on");
		$("#cate_slideList").find('section').removeAttr("slideindex").hide();
		$("#" + $(this).attr("for")).attr("slideindex", "").show();
		var nowPage = $("#cate_slideList").find("section[slideindex]").index();
		$("#cate_slider").css("-webkit-transform", "translateX(" + nowPage * 100 + "%)");
	});
	//refAppList();
});